package com.pig4cloud.pig.environment.controller;

import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pig4cloud.pig.common.core.util.R;
import com.pig4cloud.pig.common.log.annotation.SysLog;
import com.pig4cloud.pig.environment.entity.FactorEntity;
import com.pig4cloud.pig.environment.entity.ThresholdEntity;
import com.pig4cloud.pig.environment.service.FactorService;
import com.pig4cloud.pig.environment.service.ThresholdService;
import org.springframework.security.access.prepost.PreAuthorize;
import com.pig4cloud.plugin.excel.annotation.ResponseExcel;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * 因子
 *
 * @author joyql
 * @date 2024-01-15 14:40:23
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/factor" )
@Tag(description = "factor" , name = "因子管理" )
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class FactorController {

    private final  FactorService factorService;

	private final ThresholdService thresholdService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param factor 因子
     * @return
     */
    @Operation(summary = "分页查询" , description = "分页查询" )
    @GetMapping("/page" )
    @PreAuthorize("@pms.hasPermission('environment_factor_view')" )
    public R getFactorPage(@ParameterObject Page page, @ParameterObject FactorEntity factor) {
        LambdaQueryWrapper<FactorEntity> wrapper = Wrappers.lambdaQuery();
		wrapper.eq(StrUtil.isNotBlank(factor.getFactorCode()),FactorEntity::getFactorCode,factor.getFactorCode());
		wrapper.eq(StrUtil.isNotBlank(factor.getName()),FactorEntity::getName,factor.getName());
		wrapper.eq(StrUtil.isNotBlank(factor.getType()),FactorEntity::getType,factor.getType());
        return R.ok(factorService.page(page, wrapper));
    }


    /**
     * 通过id查询因子
     * @param id id
     * @return R
     */
    @Operation(summary = "通过id查询" , description = "通过id查询" )
    @GetMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('environment_factor_view')" )
    public R getById(@PathVariable("id" ) Long id) {
        return R.ok(factorService.getById(id));
    }

    /**
     * 新增因子
     * @param factor 因子
     * @return R
     */
    @Operation(summary = "新增因子" , description = "新增因子" )
    @SysLog("新增因子" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('environment_factor_add')" )
    public R save(@RequestBody FactorEntity factor) {
        return R.ok(factorService.save(factor));
    }

    /**
     * 修改因子
     * @param factor 因子
     * @return R
     */
    @Operation(summary = "修改因子" , description = "修改因子" )
    @SysLog("修改因子" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('environment_factor_edit')" )
	@Transactional(rollbackFor = Exception.class)
    public R updateById(@RequestBody FactorEntity factor) {
        Boolean res=factorService.updateById(factor);
		UpdateWrapper<ThresholdEntity> updateWrapper=new UpdateWrapper<>();
		updateWrapper.set("factor_id",factor.getId());
		ThresholdEntity thresholdEntity=new ThresholdEntity();
		thresholdEntity.setFactorCode(factor.getFactorCode());
		thresholdEntity.setFactorName((factor.getName()));
		thresholdService.update(thresholdEntity,updateWrapper);
		return R.ok(res);
    }

    /**
     * 通过id删除因子
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除因子" , description = "通过id删除因子" )
    @SysLog("通过id删除因子" )
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('environment_factor_del')" )
    public R removeById(@RequestBody Long[] ids) {
        return R.ok(factorService.removeBatchByIds(CollUtil.toList(ids)));
    }


    /**
     * 导出excel 表格
     * @param factor 查询条件
   	 * @param ids 导出指定ID
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('environment_factor_export')" )
    public List<FactorEntity> export(FactorEntity factor,Long[] ids) {
        return factorService.list(Wrappers.lambdaQuery(factor).in(ArrayUtil.isNotEmpty(ids), FactorEntity::getId, ids));
    }
}